package com.chainyoung.trademarket.ui;

import android.Manifest;
import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.view.Window;
import android.view.WindowManager;

import com.chainyoung.common.base.BaseActivity;
import com.chainyoung.common.constant.LibConstant;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.utils.CommonUtil;
import com.chainyoung.common.utils.SPUtil;
import com.chainyoung.common.utils.ToastUtil;
import com.chainyoung.router.RouterConstants;
import com.chainyoung.router.RouterUtils;
import com.chainyoung.trademarket.R;
import com.tbruyelle.rxpermissions2.RxPermissions;

import org.spongycastle.asn1.esf.SPuri;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

public class SplashActivity extends BaseActivity {


    private RxPermissions rxPermissions;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {

    }

    @Override
    protected boolean setFullScreen() {
        return true;
    }

    @Override
    protected void initUI() {
        boolean isFirst = SPUtil.getInstance().getBoolean("is_first", true);
        if (isFirst) {
            launch(GuideActivity.class);
            finish();
        } else {
            rxPermissions = new RxPermissions(this);
            rxPermissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_PHONE_STATE)
                    .subscribe(new Consumer<Boolean>() {
                        @Override
                        public void accept(@NonNull Boolean aBoolean) throws Exception {
                            if (aBoolean) {
                                Handler mHandler = new Handler();
                                mHandler.postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        jump();
                                        finish();
                                    }
                                }, 1000);
                            } else {
                                ToastUtil.showToast(getString(R.string.pls_granted_permissions));
                            }
                        }
                    });
        }

    }

    private void jump() {
        if (CommonUtil.getLoginStatus()) {
            if (!TextUtils.isEmpty(SPUtil.getString(LibConstant.PIN_CODE))) {
                RouterUtils.getInstance()
                        .build(RouterConstants.PATH_VALIDATE_CODE)
                        .withBoolean("validateFirst", true)
                        .withString("pinCode", SPUtil.getString(LibConstant.PIN_CODE))
                        .navigation(mContext);

            } else {
                launch(MainMarketActivity.class);
            }
        } else {
            launch(LoginActivity.class);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_splash;
    }
}
